package com.twq.oo.packandimpo

/**
 * Created by tangweiqun on 2017/12/1.
 * 访问修饰符
 */
object AccessModifiers {
  def main(args: Array[String]): Unit = {
    //0、如果没有被修饰的成员，默认是public的，在哪里都可以访问

    //4、private[X]
    //val navigator = new bobsrockets.navigation.Navigator()


  }
}


class Outer {

  class Inner {
    private def f() = {
      println("f")
    }

    class InnerMost {
      f() // OK }
    }

  }

  //1：private修饰的成员只能在当前的类或者对象中访问，内部类也不例外
  //(new Inner).f() // error: f is not accessible
}


package p {

  class Super {
    protected def f() = {
      println("f")
    }

    f()
  }

  class Sub extends Super {
    f()
  }

  class Other {
    //2、protected修饰的成员只能被子类访问
    //(new Super).f() // error: f is not accessible
  }

}


package bobsrockets {
  package navigation {

    //表示类Navigator只能被bobsrockets这个包里的成员访问
    private[bobsrockets] class Navigator {
      //表示方法useStarChart只能是bobsrockets.navigation中的Navigator的子类被访问
      protected[navigation] def useStarChart() = {}

      class LegOfJourney {
        private[Navigator] val distance = 100 //可以被类Navigator访问的到
      }

      new LegOfJourney().distance
      private[this] var speed = 200 //只能被当前的对象访问，其他的对象是访问不了的
    }

    object Test {
      val guide = new Navigator
      guide.useStarChart()
    }

  }

  package launch {

    import navigation._

    object Vehicle {
      private[launch] val guide = new Navigator
      guide
    }

  }

}

